package com.ywyself.controller.front;

import com.jfinal.core.ActionKey;
import com.ywyself.common.pool.DataPool;
import com.ywyself.controller.base.YsController;
import com.ywyself.ext.kit.YsKit;
import com.ywyself.model.Article;
import com.ywyself.service.ArticleService;

public class ArticleController extends YsController {

	/**
	 * 文章 - 列表
	 */
	@ActionKey("/article")
	public void index() {
		setPageTitle("文章列表");
        // 数据获取地址
        setAttr("apiPath", "/api/article");
		// 视图路径
		setAttr("viewPath", DataPool.getViewPath());
		render(DataPool.getViewPath() + "/article/index.html");
	}

	/**
	 * 文章 - 最近文章
	 */
	@ActionKey("/article/recent")
	public void recent() {
		setPageTitle("最近文章");
		// 视图路径
		setAttr("viewPath", DataPool.getViewPath());
		// 数据获取地址
		setAttr("apiPath", "/api/article/recent");
		render(DataPool.getViewPath() + "/article/index.html");
	}

	/**
	 * 文章 - 热门
	 */
	@ActionKey("/article/hot")
	public void hot() {
		setPageTitle("热门文章");
		// 视图路径
		setAttr("viewPath", DataPool.getViewPath());
		// 数据获取地址
		setAttr("apiPath", "/api/article/hot");
		render(DataPool.getViewPath() + "/article/index.html");
	}

	/**
	 * 文章 - 详细
	 */
	@ActionKey("/article/detail")
	public void detail() {
		String id = getPara(0);
		Article article = ArticleService.EXC.get(Long.parseLong(id));
		setAttr("article", article);
		setPageTitle(article.getTitle());
		// 视图路径
		setAttr("viewPath", DataPool.getViewPath());
		render(DataPool.getViewPath() + "/article/detail.html");
	}

	/**
	 * 文章 - 最近文章 - 计数
	 */
	@ActionKey("/article/recent/count")
	public void recent_count() {
		long count = ArticleService.EXC.countRecent();
		YsKit.ok().msg("共" + count + "条数据").set("count", count).render(this);
	}

	/**
	 * 文章 - 热门 - 计数
	 */
	@ActionKey("/article/hot/count")
	public void hotCount() {
		long count = ArticleService.EXC.count(true);
		YsKit.ok().msg("共" + count + "条数据").set("count", count).render(this);
	}

	/**
	 * 查询数量
	 */
	@ActionKey("/article/count")
	public void count() {
		long countRecent = ArticleService.EXC.countRecent();
		long countHot = ArticleService.EXC.count(true);
		YsKit.ok().msg("查询完成").set("countR", countRecent).set("countH", countHot).render(this);
	}
}
